const aedes = require('aedes')()
const net = require('net')

const port = 1883
const server = net.createServer(aedes.handle)

server.listen(port, function () {
  console.log('MQTT broker 已启动，监听端口:', port)
})

// 当有新的客户端连接时
aedes.on('client', function (client) {
  console.log('新客户端已连接:', client.id)
})

// 当客户端断开连接时
aedes.on('clientDisconnect', function (client) {
  console.log('客户端已断开连接:', client.id)
})

// 当收到消息时
aedes.on('publish', function (packet, client) {
  if (client) {
    console.log('收到消息:', {
      topic: packet.topic,
      payload: packet.payload.toString(),
      clientId: client.id
    })
  }
})

// 当客户端订阅主题时
aedes.on('subscribe', function (subscriptions, client) {
  console.log('客户端订阅:', {
    clientId: client.id,
    subscriptions: subscriptions
  })
})